/**
 * Copyright (c) 2009 Teoti Graphix, LLC See:
 *     http://tkarchitecture.teotigraphix.org
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 * @author Michael Schmalle mschmalle@teotigraphix.com
 */
package org.example.ui.behavior
{

import flash.events.Event;
import flash.events.FocusEvent;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;

import mx.events.SandboxMouseEvent;

import org.example.ui.component.Button;
import org.example.ui.model.IButtonModel;
import org.teotigraphix.dx.interfaces.IComponentModel;
import org.teotigraphix.tka.patterns.behavior.SparkComponentBehaviorCore;

//--------------------------------------
// ComponentHandlers
//--------------------------------------

[ComponentHandler(event="rollOver", handler="mouseEventHandler")]
[ComponentHandler(event="rollOut", handler="mouseEventHandler")]
[ComponentHandler(event="mouseDown", handler="mouseEventHandler")]
[ComponentHandler(event="mouseUp", handler="mouseEventHandler")]
[ComponentHandler(event="click", handler="mouseEventHandler")]

[ComponentHandler(event="keyUp", handler="keyUpHandler")]
[ComponentHandler(event="keyDown", handler="keyDownHandler")]

[ComponentHandler(event="focusOut", handler="focusOutHandler")]

[ModelHandler(event="mouseCapturedChange", handler="mouseCapturedHandler")]

/**
 * The core behavior abstracted from the <code>Button</code> component.
 * 
 * <p><strong>Behavior;</strong></p>
 * <ul>
 * <li>Abstracts click functionality for subclasses to enhance.</li>
 * <li>Handles component key gestures.</li>
 * <li>Handles component focus gestures.</li>
 * <li>Handles component mouse gestures.</li>
 * <li>Handles model mouse captured state changes.</li>
 * <li>Handles systemManager mouse gestures.</li>
 * </ul>
 * 
 * @copyright Teoti Graphix, LLC
 * @author Michael Schmalle
 * @date 12-08-08
 */
public class ButtonBehaviorCore 
	   extends SparkComponentBehaviorCore
{
	//--------------------------------------------------------------------------
	//
	//  Public ComponentAlias :: Variables
	//
	//--------------------------------------------------------------------------
	
	[ComponentAlias]
	
	/**
	 * The button component alias.
	 */
	public var button:Button;
	
	//--------------------------------------------------------------------------
	//
	//  Protected :: Properties
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	private var _coreModel:IButtonModel;
	
	/**
	 * The button's <code>coreModel</code>.
	 */
	protected function get coreModel():IButtonModel
	{
		return _coreModel;
	}
	
	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function ButtonBehaviorCore()
	{
		super();
	}
	
	//--------------------------------------------------------------------------
	//
	//  Overridden Protected :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Sets the <code>coreModel</code> when a <code>IButtonModel</code>
	 * is set as the <code>button.coreModel</code>.
	 * 
	 * @inheritDoc
	 */
	override protected function modelAdded(api:Class, model:IComponentModel):void
	{
		super.modelAdded(api, model);
		
		if (model is IButtonModel)
			_coreModel = IButtonModel(model);
	}
	
	/**
	 * unsets the <code>coreModel</code> when a <code>IButtonModel</code>
	 * is unset from the <code>button.coreModel</code>.
	 * 
	 * @inheritDoc
	 */
	override protected function modelRemoved(api:Class, model:IComponentModel):void
	{
		super.modelRemoved(api, model);
		
		if (model is IButtonModel)
			_coreModel = null;		
	}
	
	//--------------------------------------------------------------------------
	//
	//  Protected :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Override in subclasses to a click behavior.
	 */	
	protected function onClick(event:MouseEvent):void
	{
	}
	
	//--------------------------------------------------------------------------
	//
	//  component_handler :: Handlers
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	//  Keyboard
	//----------------------------------
	
	/**
	 * Handles the <code>KeyboardEvent.KEY_DOWN</code> event.
	 */
	component_handler function keyDownHandler(event:KeyboardEvent):void
	{
		if (event.keyCode != Keyboard.SPACE)
			return;
		
		coreModel.keyboardPressed = true;
		
		event.updateAfterEvent();
	}
	
	/**
	 * Handles the <code>KeyboardEvent.KEY_UP</code> event.
	 */
	component_handler function keyUpHandler(event:KeyboardEvent):void
	{
		if (event.keyCode != Keyboard.SPACE)
			return;
		
		coreModel.keyboardPressed = false;
		
		if (button.enabled)
			button.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
		
		event.updateAfterEvent();
	}
	
	/**
	 * Handles the <code>FocusEvent.FOCUS_OUT</code> event.
	 */
	component_handler function focusOutHandler(event:FocusEvent):void
	{
		coreModel.mouseCaptured = false;
		coreModel.keyboardPressed = false;
	}
	
	//----------------------------------
	//  Mouse
	//----------------------------------
	
	/**
	 * The handler for the Component mouse events.
	 * 
	 * @param event A MouseEvent.
	 */
	component_handler function mouseEventHandler(event:Event):void
	{
		var mouseEvent:MouseEvent = event as MouseEvent;
				
		switch (event.type)
		{
			case MouseEvent.ROLL_OVER:
			{
				if (mouseEvent.buttonDown && !coreModel.mouseCaptured)
					return;
				
				coreModel.hoveredOver = true;
				break;
			}
			case MouseEvent.ROLL_OUT:
			{
				coreModel.hoveredOver = false;
				break;
			}
			case MouseEvent.MOUSE_DOWN:
			{
				coreModel.mouseCaptured = true;
				break;
			}
			case MouseEvent.MOUSE_UP:
			{
				if (event.currentTarget == this)
					coreModel.hoveredOver = true;
			}// fall through
			case Event.MOUSE_LEAVE:
			{
				coreModel.mouseCaptured = false;
				break;
			}
			case MouseEvent.CLICK:
			{
				if (!button.enabled)
					event.stopImmediatePropagation();
				else
					onClick(MouseEvent(event));
				return;
			}
		}
		if (mouseEvent)
			mouseEvent.updateAfterEvent();
	}
	
	//----------------------------------
	//  Model
	//----------------------------------
	
	/**
	 * Handler for the <code>mouseCaptured</code> property.
	 * 
	 * @see org.example.model.IButtonModel#mouseCaptured
	 * @see org.example.model.ButtonModelCore#mouseCaptured
	 */
	model_handler function mouseCapturedHandler(event:Event):void
	{
		if (!coreModel.mouseCaptured)
			removeSystemMouseHandlers();
		else
			addSystemMouseHandlers();
	}
	
	//--------------------------------------------------------------------------
	//
	//  Private systemManager :: Handlers
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */	
	private function addSystemMouseHandlers():void
	{
		button.systemManager.getSandboxRoot().addEventListener(
			MouseEvent.MOUSE_UP, 
			component_handler::mouseEventHandler, true /*useCapture*/);
		button.systemManager.getSandboxRoot().addEventListener(
			SandboxMouseEvent.MOUSE_UP_SOMEWHERE, 
			component_handler::mouseEventHandler);
	}
	
	/**
	 * @private
	 */	
	private function removeSystemMouseHandlers():void
	{
		button.systemManager.getSandboxRoot().removeEventListener(
			MouseEvent.MOUSE_UP, 
			component_handler::mouseEventHandler, true /*useCapture*/);
		button.systemManager.getSandboxRoot().removeEventListener(
			SandboxMouseEvent.MOUSE_UP_SOMEWHERE, 
			component_handler::mouseEventHandler);
	}
}
}